﻿using System;
using System.ServiceModel;
using System.Threading;
using System.Windows.Forms;
using FileManager_Server.BusinessLogic.IRepository_Service;
using FileManager_Server.BusinessLogic.WCF.ObjectContainer_Setting;
using FileManager_Server.BusinessLogic.WCF.System_Setting_Service;

namespace FileManager_Server.PresentationLayer
{
    public partial class FileManagerServer : Form
    {
        delegate void ChangeUI(); 
        private ServiceHostCollection _serviceHost;
        private ISystemService systemService;
        public FileManagerServer()
        {
            InitializeComponent();
            ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<ISystemService>().InitializeCastle();
            richTextBoxEx1.LinkClicked += richTextBoxEx1_LinkClicked;
            richTextBoxEx1.SelectedText = "With this extended RichTextBox you're able to insert\n";
            richTextBoxEx1.SelectedText = "your own arbitrary links in the text: ";
            richTextBoxEx1.InsertLink("Link with arbitrary text");
            richTextBoxEx1.SelectedText = ".\nYou are not limited to the standard protocols any more,\n";
            richTextBoxEx1.SelectedText = "but you can still use them, of course: ";
            richTextBoxEx1.InsertLink("http://www.codeproject.com");
            richTextBoxEx1.SelectedText = "\n\nThe new links fire the LinkClicked event, just like the standard\n";
            richTextBoxEx1.SelectedText = "links do when AutoDetectUrls is set.\n\n";
            richTextBoxEx1.SelectedText = "Managing hyperlinks independent of link text is possible as well: ";
            richTextBoxEx1.InsertLink("Link text", "Hyperlink text");
        }

        void richTextBoxEx1_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            MessageBox.Show("A link has been clicked.\nThe link text is '" + e.LinkText + "'");
        }

        private void Btn_StartService_Click(object sender, EventArgs e)
        {
            systemService =
                ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<ISystemService>();
          //  _serviceHost = systemService.StartWcfService();
            Lb_Message.Text = "已启动服务";
        }

        private void Btn_StopService_Click(object sender, EventArgs e)
        {
            var threadArgs = new ThreadArgs
                                 {
                                     ServiceHost = _serviceHost,
                                     SystemService = systemService
                                 };
            var parStart = new ParameterizedThreadStart(StopService);
            var stopThread = new Thread(parStart);
            stopThread.Start(threadArgs);
            Lb_Stop_Message.Text = "正在停止服务";
        }

        private void StopService(object args)
        {
            var threadArgs = args as ThreadArgs;
            if (null != threadArgs)
            {
                if (null != threadArgs.SystemService)
                    threadArgs.SystemService.StopWcfService(_serviceHost);
            }
            if (!Lb_Message.InvokeRequired) return;
            var changeUi = new ChangeUI(DelegateSetValue);
            Lb_Message.Invoke(changeUi);
        }
        void DelegateSetValue()
        {
            Lb_Message.Text = "服务已停止";
            Lb_Stop_Message.Text = "....";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Test");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }

        private void Btn_NotifyMessage_Click(object sender, EventArgs e)
        {
            BeginInvoke((EventHandler) delegate
                                           {
                                               systemService.NotifyMessage("Say Hi!");
                                           });
            MessageBox.Show("通知成功!");
        } 
    }

    public class ThreadArgs
    {
        public ServiceHostCollection ServiceHost { get; set; }
        public ISystemService SystemService { get; set; }
    }
}
